package com.cloudbroker.bcs.platform.dav.middle.model;

import com.cloudbroker.bcs.common.util.MD5Util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.TreeMap;

/**
 * Created by wangxu13798 on 2016/11/24.
 */
public class ParamEncrypt {
    private TreeMap<String,Object> treeMap = new TreeMap<>();

    private String sign;

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void putParam(String key,String value){
        try {
            this.treeMap.put(key,URLEncoder.encode(value,"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public boolean checkSign(){
        return getSign().equals(MD5Util.encrypt(MD5Util.dictSortTL(treeMap)));
    }

    public String sign(){
        return MD5Util.encrypt(MD5Util.dictSortTL(treeMap));
    }
}
